<?php
if ( ! defined('BASEPATH')) exit('No direct script access allowed');

/**
 *
 *
 */
class json extends CI_Model  {
	/**
	 * Constructor
	 * @access protected
	 */
	function _construct()
	{
		// Call the Model constructor
		parent::_construct();
	}
	function getlist($client,$agent,$type)
	{
		
		$option=array(
					3=>array('nights'=>6,'city'=>31),
					1=>array('nights'=>14,'city'=>13),
					2=>array('nights'=>30,'city'=>29)
					);
		$query='select idUser from podpisannie where idUser='.$agent.' and idPodp='.$client;
		$result=$this->db->query($query);
		$responce=array();
		if($result->num_rows>0||$agent==$client)
		{
			$query='select id,name from service where id in (select idService from listofservices where idUser='.$agent.') and idtype='.$type;
			#$this->log($query);
			$result=$this->db->query($query);
			foreach($result->result() as $row)
			{
				$responce[$row->id]['name']=$row->name;
				$responce[$row->id]['id']=$row->id;
				$query='select url from fotos where id in (select idfoto from listfotos where idservice='.$row->id.')';
				$fotos=$this->db->query($query);
				$responce[$row->id]['fotos']='';
				foreach($fotos->result() as $foto)
				{
					$responce[$row->id]['fotos'][]="http://rustmarket.com/images/photo/".$foto->url;
				}
				if($responce[$row->id]['fotos']=='')
				{
					$responce[$row->id]['fotos'][]="http://rustmarket.com/images/no.png";
				}
				$query='select `desc` from listoptions where idservice='.$row->id." and idoption=".$option[$type]['nights'];
				#$this->log($query);
				$night=$this->db->query($query);
				if($night->num_rows()>0)
				{
					$responce[$row->id]['nights']=$night->row()->desc;
				}
				else
				{
					$responce[$row->id]['nights']='';
				}
				$query='select `name` from service where id in (select `desc` from listoptions where idoption='.$option[$type]['city'].' and idservice='.$row->id.')';
				#$this->log($query);
				$city=$this->db->query($query);
				$responce[$row->id]['city']=array();
				foreach($city->result() as $c)
				{
				
					$responce[$row->id]['city'][]=$c->name;
				}
				
				$query='SELECT min(price) as mn, max(price) as mx FROM price where idRow=1 and idtable in (select id from listoftables where idservice='.$row->id.');';
				
				$price=$this->db->query($query);
				if($price->num_rows()>0)
				{
					$responce[$row->id]['min_p']=$price->row()->mn;
					$responce[$row->id]['max_p']=$price->row()->mx;
				}
				else
				{
					$responce[$row->id]['min_p']='';
					$responce[$row->id]['max_p']='';
				}
			}
		}
		return json_encode($responce);
	}

	function getfull($type,$id)
	{
		switch($type)
		{
			case 3:
				$responce=array();
				#Hotels name
				$query="select id,name from service where id in (select `desc` from listoptions where idservice=".$id." and idoption=8)";
				$this->log($query);
				$result=$this->db->query($query);
				foreach($result->result() as $row)
				{
					$responce['hotel'][$row->id]=$row->name;
				}
				#Exkursions
				$query="select id,name from service where id in (select `desc` from listoptions where idservice=".$id." and idoption=2)";
				$this->log($query);
				$result=$this->db->query($query);
				foreach($result->result() as $row)
				{
					$responce['excurs'][$row->id]=$row->name;
				}
				# get full description
				$query='select `desc` from service where id='.$id;
				$result=$this->db->query($query);
				$responce['description']=$result->row()->desc;
				//return $responce;
				return json_encode($responce);
			break;
			case 1:
				$responce=array();
				#description and all options
				$query="select `desc` from service where id=".$id;
				$result=$this->db->query($query);
				$responce['description']=$result->row()->desc;
				#select all options from this service
				$query="SELECT idoption, `desc` FROM listoptions l where idservice=".$id;
				$result=$this->db->query($query);
				$responce['in_h_ico']="";
				$responce['in_r_ico']="";
				
				foreach($result->result() as $row)
				{
					switch($row->idoption)
					{
						case '15':
							$responce['place']=$row->desc;
						break;
						case '17':
							$responce['in_hotel']=$row->desc;
						break;
						case '19':
							$responce['in_room']=$row->desc;
						break;
						case '16':
							$query="select `name`,`rules` from options where id=".$row->desc;
							$res=$this->db->query($query);
							foreach($res->result() as $r)
							{
								$responce['in_h_ico'][]=$r->name;
							}	
						break;
						case '18':
							$query="select `name`,`rules` from options where id=".$row->desc;
							$res=$this->db->query($query);
							foreach($res->result() as $r)
							{
								$responce['in_r_ico'][]=$r->name;
							}
						break;
					}
					//$responce['excurs'][$row->id]=$row->name;
				}
				
				//return $responce;
				return json_encode($responce);
			break;
			
		}
	}

	function get($y,$m,$id)
	{
		$responce=array();
		$n=($m!=2?(($m%2)^($m>7))+30:(!($y%400)||!($y%4)&&($y%25)?29:28));
		$dateS=$y."-".$m."-01";
		$dateE=$y."-".$m."-".$n;
		
		$query="select name from service where id=".$id;
		$result=$this->db->query($query);
		if($result->num_rows()>0)
		{
			$name=$result->row()->name;
		}
		else
		{
			die('hack');
		}
		$responce['serviceName']=$name;
		$responce['serviceId']=$id;
		$query="Select id,idStruct,name from listoftables where idService=".$id;
		$result=$this->db->query($query);
		$table=0;
		foreach($result->result() as $row)
		{
			
			$idstruct=$row->idStruct;
			$tableName=$row->name;
			$idtable=$row->id;
			$responce['tables'][$table]['name']=$tableName;
			$responce['tables'][$table]['id']=$idtable;
			
			$query="Select idrow from liststruct where idStruct=".$idstruct;
			$result=$this->db->query($query);
			$i=0;
			foreach($result->result() as $row)
			{
				$query="Select id,name,`desc` from rows where id=".$row->idrow;
				$rowsName=$this->db->query($query);
				if($rowsName->num_rows()>0)
				{
					$row=$rowsName->row();
					$struct[$i]['id']=$row->id;
					$struct[$i]['name']=$row->name;
					$struct[$i]['desc']=$row->desc;
					$i++;
				}
			}
			//$query='select name from '
			$query="Select `Date`,`price`,`idRow` from price where idTable=".$idtable." and `date`>='$dateS' and `date`<='$dateE'";
			$result=$this->db->query($query);
			
			$DATA=array();
			$i=0;
			foreach($result->result() as $row)
			{
				$DATA[$i]['price']=$row->price;
				$DATA[$i]['date']=$row->Date;
				$DATA[$i]['idRow']=$row->idRow;
				$i++;
			}
			$rows=array();
			
			for($i=0;$i<count($struct);$i++)
			{
				$rows[$i]['cell']=array($struct[$i]['id'],$struct[$i]['name'],$struct[$i]['desc']);
				for($j=3;$j<=$n+2;$j++)
				{
					if($price=$this->_search($j-1,$m,$y,$struct[$i]['id'],$DATA))
					{
						$rows[$i]['cell'][$j]=$price==0?"":$price;
					}
					else
					{
						$rows[$i]['cell'][$j]="";
					}
				}
			}
			$responce['tables'][$table]['rows']=$rows;
			$table++;
		}
		
		/*
		if($result->num_rows()>0)
		{
			$idstruct=$result->row()->idStruct;
		}
		else{$this->log('getPrice Error table id='.$id);return false;}
		$query="Select idrow from liststruct where idStruct=".$idstruct;
		$result=$this->db->query($query);
		$i=0;
		foreach($result->result() as $row)
		{
			$query="Select id,name,`desc` from rows where id=".$row->idrow;
			$rowsName=$this->db->query($query);
			if($rowsName->num_rows()>0)
			{
				$row=$rowsName->row();
				$struct[$i]['id']=$row->id;
				$struct[$i]['name']=$row->name;
				$struct[$i]['desc']=$row->desc;
				$i++;
			}
		}
		//$query='select name from '
		$query="Select `Date`,`price`,`idRow` from price where idTable=".$id." and `date`>='$dateS' and `date`<='$dateE'";
		$result=$this->db->query($query);
		$i=0;
		
		$i=0;
		foreach($result->result() as $row)
		{
			$DATA[$i]['price']=$row->price;
			$DATA[$i]['date']=$row->Date;
			$DATA[$i]['idRow']=$row->idRow;
			$i++;
		}
		
		
		for($i=0;$i<count($struct);$i++)
		{
			$rows[$i]['cell']=array($struct[$i]['id'],$struct[$i]['name'],$struct[$i]['desc']);
			for($j=3;$j<=$n+2;$j++)
			{
				if($price=$this->_search($j-1,$m,$y,$struct[$i]['id'],$DATA))
				{
					$rows[$i]['cell'][$j]=$price==0?"":$price;
				}
				else
				{
					$rows[$i]['cell'][$j]="";
				}
			}
		}
		$responce->rows=$rows;
		$this->db->close();*/
		return json_encode($responce);
	}
	function _search($dd,$mm,$yyyy,$idRow,$DATA)
	{
		$date=$yyyy;
		if (strlen ($mm)<2) {
			$date.="-0".$mm;
		}
		else
		{
			$date.="-".$mm;
		}
		if (strlen ($dd)<2) {
			$date.="-0".$dd;
		}
		else
		{
			$date.="-".$dd;
		}
		for ($i=0;$i<count($DATA);$i++)
		{
		if(strcmp ($DATA[$i]['date'],$date)==0&&$DATA[$i]['idRow']==$idRow)//
		{
		return $DATA[$i]['price'];
				}
		}
		return false;
	}
}